package cn.yj.core.business;

import cn.yj.config.annotation.Anonymous;
import cn.yj.config.web.AppBaseController;
import cn.yj.wx.pay.config.WxPayConfig;
import cn.yj.wx.pay.enrity.NoticeBody;
import cn.yj.wx.pay.utils.WxPayUtils;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 永健
 * @since 2021-09-23 13:38
 */
@RequestMapping("/pay")
@RestController
public class PayController extends AppBaseController {

    private static final Logger log = LoggerFactory.getLogger(PayController.class);

    @Autowired(required = false)
    private WxPayConfig payConfig;

    /**
     * 支付结果通知
     *
     * @param body
     */
    @RequestMapping("/wxPayNotice")
    @Anonymous
    public Map<String, Object> wxPayNotice(@RequestBody NoticeBody body) {
        System.out.println(body);

        Map<String, Object> map = getMap("code", "SUCCESS");
        map.put("message", "成功");

        try {
            JSONObject jsonObject = WxPayUtils.checkPayNotice(body, payConfig.getApiV3Key());
            // TODO 业务
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "ERROR");
            map.put("message", "失败");
        }
        log.info("支付结果回掉");
        return map;
    }
}
